<div id="register-dialog">
    <form id="register-form">
        <div id="register-tips"></div>
        <div class="input-group">
            <!--<label for="register-name" class="input-group-addon">Email</label>-->
            <input type="email" name="register_name" id="register-name" class="form-control" minlength="3" maxlength="30" required="required" placeholder="请填写你的邮箱"/>
        </div>
        <div class="input-group">
            <!--<label for="register-password" class="input-group-addon">密码</label>-->
            <input type="password" name="register_password" id="register-password" class="form-control" minlength="6" maxlength="30" required="required" placeholder="请填写你的密码"/>
        </div>
        <div class="input-group">
            <input type="text" name="register_captcha" id="register-captcha" class="form-control" minlength="4" maxlength="30" required="required" placeholder="请填写验证码" style="width:180px;"/>
            <div class="register-captcha-img">
                <?php if (isset($captcha) && isset($captcha['image'])) echo $captcha['image']; ?>
            </div>
            <div class="clearfix"></div>
            <div class="input-group">
                <input type="submit" class="btn btn-success form-control" value="注册"/>
            </div>
            <div class="left-right-side">
                <span class="right-side"><a class="signIn" href="javascript:">已有账号？立即登录</a></span>
            </div>
        </div>
    </form>
</div>

<script>
    $(function () {
        $.AutoComplete('#register-name');

        $('.register-captcha-img').click(function () {
            $.getJSON('<?php echo base_url('api/getCaptcha');?>', function (rsp) {
                try {
                    switch (rsp.type) {
                        case 'success':
                            $('.register-captcha-img').html(rsp.data);
                            break;
                        default :
                            noty({type: rsp.type, text: rsp.message});
                            break;
                    }
                } catch (ex) {
                    noty({type: 'error', text: '发生内部错误，请联系管理员'});
                    throw ex;
                }
            });
        });
        $("#register-form").validate({
            submitHandler: function (form) {
                $.ajax({
                    url: '<?php echo base_url('api/register');?>',
                    type: 'post',
                    data: $(form).serialize(),
                    beforeSend:function(){
                        showProgressNoty();
                    },
                    complete: function () {
                        closeProgressNoty();
                    },
                    success: function (jsonrsp) {
                        try {
                            var rsp = $.parseJSON(jsonrsp);
                            noty({type: rsp.type, text: rsp.message, callback: {
                                afterClose: function () {
                                    switch (rsp.type) {
                                        case 'success':
                                            noty({
                                                type:'warning',
                                                text:'邮件已发送至您邮箱，现在去激活吗？',
                                                buttons: [
                                                    {
                                                        addClass: 'btn btn-sm btn-danger', text: '确定',
                                                        onClick: function ($noty) {
                                                            $noty.close();
                                                            window.location.href=rsp.mail_url;
                                                        }
                                                    },
                                                    {
                                                        addClass: 'btn btn-sm btn-default', text: '取消',
                                                        onClick: function ($noty) {
                                                            $noty.close();
                                                            location.reload();
                                                        }
                                                    }
                                                ]
                                            });
                                            break;
                                        case 'fail':
                                            noty({type:'error',text:'邮件发送失败，请联系管理员！'});
                                        default:
                                            break;
                                    }
                                }
                            }});
                        } catch (ex) {
                            noty({type: 'error', text: '发生内部错误，请联系管理员'});
                            throw ex;
                        }
                    }
                });
            }
        });
    });
</script>